package com.itheima.app.controller;

import com.itheima.app.exception.BusinessExcepiton;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.UserManager;
import com.itheima.domain.db.User;
import com.itheima.domain.db.UserInfo;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
//import sun.misc.Request;

import java.io.IOException;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    UserManager userManager;

    @PostMapping("/user/save")
    public Long save(@RequestBody User user) {
        return userManager.save(user);
    }

    @GetMapping("/user/findByMobile")
    public User findByMobile(String mobile) {

        return userManager.findByMobile(mobile);
    }

    //发送验证码
    @PostMapping("/user/login")
    public void sendCode(@RequestBody Map<String, String> map) {
        //1.获取手机号
        String phone = map.get("phone");
        //2.调用userManger中的方法实现操作
        userManager.sendCode(phone);
    }

    //注册或登陆
    @PostMapping("/user/loginVerification")
    public ResponseEntity regAndLogin(@RequestBody Map<String, String> map) throws BusinessExcepiton {
        return userManager.regAndLogin(map);
    }

    //完善用户信息
    @PostMapping("/user/loginReginfo")
    public ResponseEntity saveUserInfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token) {
        return userManager.saveUserInfo(userInfo, token);
    }

    // 更新用户头像
    @PostMapping({"/user/loginReginfo/head", "/users/header"})
    public ResponseEntity saveUserInfoHead(@RequestHeader("Authorization") String token, MultipartFile headPhoto) throws IOException {
        return userManager.saveUserInfoHead(token, headPhoto);
    }

    //查询用户信息
    @GetMapping("/users")
    public ResponseEntity findUserInfoById(@RequestHeader("Authorization") String token, @RequestParam(required = false) Long userID) {
        //1.判断userID是否为空,若不为空,调用manager查询
        if (userID != null) {
            return userManager.findUserInfoById(userID);
        } else {
          /*
          //2.若userID为空,从token中获取用户,调用manager查询
            User user = userManager.findUserFromToken(token);

            if (user == null) {
                return ResponseEntity.status(401).body(null);
            }
          */

            User user = UserHolder.get();
            return userManager.findUserInfoById(user.getId());
        }
    }

    //更新用户信息
    @PutMapping("/users")
    public ResponseEntity updateUserInfo(@RequestHeader("Authorization") String token, @RequestBody UserInfo userInfo) {
        return userManager.updateUserInfo(token, userInfo);
    }


    //统计 互相喜欢，喜欢，粉丝数量
    @GetMapping("/users/counts")
    public ResponseEntity selectUserLikeCount() {
        return userManager.selectUserLikeCount();
    }


    //互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
    @GetMapping("/users/friends/{type}")
    public ResponseEntity selectFriends(
            @PathVariable("type") Integer type,
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize

    ) {
        return userManager.selectFriends(pageNum, pageSize, type);
    }


    @PostMapping("/users/fans/{uid}")
    public void like(@PathVariable("uid") Integer uid) {
        userManager.like(uid);
    }


    @DeleteMapping("/users/like/{uid}")
    public void nolike(@PathVariable("uid") Integer uid) {
        userManager.noLike(uid);
    }
}
